package com.adobe.apprise.commands
{
	import com.adobe.apprise.database.*;
	import com.adobe.apprise.model.ModelLocator;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.resources.ResourceManager;
	
	public class RefreshCountsCommand implements ICommand
	{
		public function execute(ce:CairngormEvent):void
		{
			var ml:ModelLocator = ModelLocator.getInstance();
			
			var db:Database = ml.db;

			// Total unread and total checked counts.

			var unreadResponder:DatabaseResponder = new DatabaseResponder();

			unreadResponder.addEventListener(DatabaseEvent.RESULT_EVENT,
				function(unreadEvent:DatabaseEvent):void
				{
					for each (var view:Object in ml.totals.source)
					{
						if (view.viewName == ResourceManager.getInstance().getString('resources', 'VIEWCONTROL_ALL_UNREAD'))
						{
							view.count = unreadEvent.data;
							ml.totals.itemUpdated(view, "count");
							break;
						}
					}
				});
			db.getUnreadCount(unreadResponder);

			var checkedResponder:DatabaseResponder = new DatabaseResponder();
			checkedResponder.addEventListener(DatabaseEvent.RESULT_EVENT,
				function(checkedEvent:DatabaseEvent):void
				{
					for each (var view:Object in ml.totals.source)
					{
						if (view.viewName == ResourceManager.getInstance().getString('resources', 'VIEWCONTROL_CHECKED'))
						{
							view.count = checkedEvent.data;
							ml.totals.itemUpdated(view, "count");
							break;
						}
					}
				});
			db.getCheckedCount(checkedResponder);


			// Feeds.

			var feedsResponder:DatabaseResponder = new DatabaseResponder();
			feedsResponder.addEventListener(DatabaseEvent.RESULT_EVENT,
				function(feedsEvent:DatabaseEvent):void
				{
					for each (var feed:Object in ml.feeds.source)
					{
						feed.unread = 0;
						for each (var o:Object in feedsEvent.data)
						{
							if (feed.name == o.name)
							{
								feed.unread = o.unread;
								break;
							}
						}
						ml.feeds.itemUpdated(feed, "unread");
					}
				});
			db.getUnreadFeeds(feedsResponder);


			// Authors.

			var authorsResponder:DatabaseResponder = new DatabaseResponder();
			authorsResponder.addEventListener(DatabaseEvent.RESULT_EVENT,
				function(authorEvent:DatabaseEvent):void
				{
					for each (var author:Object in ml.authors.source)
					{
						author.unread = 0;
						for each (var o:Object in authorEvent.data)
						{
							if (author.author == o.author)
							{
								author.unread = o.unread;
								break;
							}
						}
						ml.authors.itemUpdated(author, "unread");
					}
				});
			db.getUnreadAuthors(authorsResponder);


			// Topics.

			var topicsResponder:DatabaseResponder = new DatabaseResponder();
			topicsResponder.addEventListener(DatabaseEvent.RESULT_EVENT,
				function(topicsEvent:DatabaseEvent):void
				{
					for each (var topic:Object in ml.topics.source)
					{
						topic.unread = 0;
						for each (var o:Object in topicsEvent.data)
						{
							if (topic.topic == o.topic)
							{
								topic.unread = o.unread;
								break;
							}
						}
						ml.topics.itemUpdated(topic, "unread");
					}
				});
			db.getUnreadTopics(topicsResponder);
		}
	}
}
